/**
 * 
 */
package com.dy.agent.agent.api.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dy.agent.agent.api.biz.AgentAnchorBiz;
import com.dy.agent.agent.api.model.dto.StatisticsQueryCondition;
import com.dy.agent.agent.api.model.entiry.AgentAnchorStatistics;
import com.dy.agent.agent.api.model.entiry.AgentDailyView;
import com.dy.agent.agent.api.model.entiry.AnchorHistory;
import com.dy.agent.agent.api.model.vo.AnchorDailyIncomeView;
import com.dy.agent.agent.api.model.vo.AnchorMonthIncomeView;
import com.dy.agent.agent.api.model.vo.AnchorPlayLengthView;
import com.dy.agent.agent.api.service.AgentAnchorStatisticsService;
import com.dy.agent.agent.api.service.AgentDailyViewService;
import com.dy.agent.agent.api.service.AnchorHistoryService;
import com.dy.agent.agent.api.web.controller.utils.ControllerUtils;
import com.dy.agent.agent.api.web.controller.utils.ResultUtils;
import com.dy.agent.agent.api.web.controller.vo.ResultWrapper;
import com.google.common.collect.Maps;

/**
 * @author leiruiqi
 *
 */
@RestController
@RequestMapping("/agentStatistics")
public class AgentAnchorStatisticsController {

	@Resource
	private AgentDailyViewService agentDailyViewService;

	@Resource
	public AgentAnchorStatisticsService agentAnchorStatisticsService;

	@Resource
	private AgentAnchorBiz agentAnchorBiz;
	
	@Resource
	private AnchorHistoryService anchorHistoryService;

	@RequestMapping("/agentCurrentResult")
	public ResultWrapper<?> getAgentDailyView(final Long gid) {

		return ControllerUtils.doHandler(new ControllerUtils.ActionHandler() {

			@Override
			public ResultWrapper<?> getResponse() {

				AgentDailyView view = agentDailyViewService.getLastViewByGid(gid);
				return ResultUtils.asSuccess(view);
			}

		}, "AgentAnchorStatisticsController.getAgentDailyView");
	}

	@RequestMapping("/agnetAnchorDailyIncomRecord")
	public ResultWrapper<?> agnetAnchorDailyIncomRecord(final Long gid, final Long uid, final String startDt,
			final String endDt) {

		return ControllerUtils.doHandler(new ControllerUtils.ActionHandler() {

			@Override
			public ResultWrapper<?> getResponse() {
				StatisticsQueryCondition condition = new StatisticsQueryCondition();
				condition.setStartDt(startDt);
				condition.setEndDt(endDt);
				condition.setGid(gid);
				condition.setUid(uid);

				List<AgentAnchorStatistics> list = agentAnchorStatisticsService.queryListByCondition(condition);

				List<AnchorDailyIncomeView> viewList = agentAnchorBiz.converRecord2AnchorIncomeView(list);

				return ResultUtils.asSuccess(viewList);
			}

		}, "AgentAnchorStatisticsController.getAgentDailyView");
	}

	@RequestMapping("/agentAnchorDailyPlayResult")
	public ResultWrapper<?> agentAnchorDailyPlayResult(final Long gid) {

		return ControllerUtils.doHandler(new ControllerUtils.ActionHandler() {

			@Override
			public ResultWrapper<?> getResponse() {

				Map<Long, AnchorPlayLengthView> map = agentAnchorBiz.buildAnchorLast2Month4PlayLength(gid, null);

				return ResultUtils.asSuccess(map.values());
			}

		}, "AgentAnchorStatisticsController.getAgentDailyView");
	}

	@RequestMapping("/agentAnchorMonthRecord")
	public ResultWrapper<?> agentAnchorMonthRecord(final Long gid, final String month) {

		return ControllerUtils.doHandler(new ControllerUtils.ActionHandler() {

			@Override
			public ResultWrapper<?> getResponse() {

				Map<Long, AnchorMonthIncomeView> map = agentAnchorBiz.listAgentAnchorMonthIncome(gid, month);
				Long allMonthIncome = 0L;
				for (AnchorMonthIncomeView view : map.values()) {
					allMonthIncome += view.getIncome();
				}
				Map<String, Object> data = Maps.newHashMap();
				data.put("month", month);
				data.put("income", allMonthIncome);
				data.put("anchorIncomeList", map.values());
				return ResultUtils.asSuccess(data);
			}

		}, "AgentAnchorStatisticsController.agentAnchorMonthRecord");
	}

	@RequestMapping("/anchorHistory")
	public ResultWrapper<?> anchorHistory(final Long uid) {

		return ControllerUtils.doHandler(new ControllerUtils.ActionHandler() {

			@Override
			public ResultWrapper<?> getResponse() {

				AnchorHistory anchorHistory = anchorHistoryService.getByUid(uid);
				Map<String,Object> data = Maps.newHashMap();
				if(anchorHistory!=null){
					data.put("income", anchorHistory.getDay90Amount());
				}else{
					data.put("income", null);
				}
				
				return ResultUtils.asSuccess(data);
			}

		}, "AgentAnchorStatisticsController.anchorHistory");
	}

}
